/*$(function(){*/
	//执行showTime()  
	var cache={};
	//查看订单
	 $.ajax({
			  type:"POST",
			  url:"/movie.front/orderController/getOrderByUserId",
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){   
				
				  if(data.result[0].statusCode!=null){
					  //跳转到登录页面
					  alert(data.result[0].statusCode);
					window.location.href="account.html";
				  }else{
					  //页面跳转到订单页面   		
					 var arr =  data.result;
					 cache.hallId = arr[0].hallId;
					for(var i=0;i<arr.length;i++){
					 var html='';
					 var seats = arr[i].orderSeats;
					      seats = seats.substring(0,seats.length-1) ;
					html = html+'<tr style="border:0px solid;"><td style="width:200px;height:100px;border-bottom:1px solid ;border-color: #cdd9e0;">'+arr[i].id+'</td><td style="width:200px;height:100px;border-bottom:1px solid ;border-color: #cdd9e0;">'+arr[i].orderName+'</td><td style="width:100px;hieght:100px;border-bottom:1px solid;border-color: #cdd9e0;">'+arr[i].orderPrice+'元</td><td style="width:300px;hieght:100px;border-bottom:1px solid;border-color: #cdd9e0;">'+seats+'</td><td style="width:300px;hieght:100px;border-bottom:1px solid;border-color: #cdd9e0;">'+arr[i].orderTime+'</td></tr>';
					$('#myTable-order').append(html);
					
					}
					showTime();//添加时间
				  }
			  }
			   
		   });
/*});*/

 var totalTime =15;
function showTime(){
	   totalTime -= 1;
	   var m = Math.floor(totalTime/60);
	   var s = totalTime%60;
	   $('#orderTime').empty();
	  $('#orderTime').append( '<font size="12px">请于'+m+'分'+s+'秒之内完成支付'+'</font>');
	   if(totalTime<=0){
			 //订单取消
		    var array = arrOrder();
		    var arraySeats = arrOrderSeats();
			 cancelOrder(array,arraySeats);
			 updateSeats(arraySeats);
			 alert("对不起，您的订单未能及时支付，已经取消，请重新购买");
			
			// 
			 //页面跳转
			  window.location.href="products.html";
	   }else{
	   setTimeout("showTime()",1000);
	   //提交订单
	   
	    $('#apply-success').click(function(){
	    	 var array = arrOrder();
	    	 successOrder(array);
	    });
	  }
	   }  

function successOrder(arr){
	//查看订单
	 $.ajax({
			  type:"POST",
			  url:"/movie.front/orderController/updateSubmitOrderById",
			  data:JSON.stringify({list:arr}),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){   
				window.location.href="success.html";
			  }
			   
		   });
}


function cancelOrder(arr){
	//查看订单
	 $.ajax({
			  type:"POST",
			  url:"/movie.front/orderController/updateOrderById",
			  data:JSON.stringify({list:arr}),
			  dataType:"json",
			  contentType:"application/json",
			  success:function(data){   
				
			  }
			   
		   });
}

//提交所有订单
function arrOrder(){
	 var array =new Array(); 
		for (var i = 0; i <$("#myTable-order")[0].rows.length; i++) {    //遍历Table的所有Row
		        tableInfo = $("#myTable-order")[0].rows[i].cells[0].innerText;   //获取Table中单元格的内容
		     //  alert(tableInfo);
		        array.push(tableInfo);
		}
		 array.splice(0,1);
		 
		 return array;
}

function updateSeats(arr){
	var hallId = cache.hallId;
	var p = {"list":arr,"hallId":hallId}
	 $.ajax({
		  type:"POST",
		  url:"/movie.front/orderController/updateSeatsById",
		  data:JSON.stringify(p),
		  dataType:"json",
		  contentType:"application/json",
		  success:function(data){   
			
		  }
		   
	   });
}
//查看座位信息
function arrOrderSeats(){
	 var array =new Array(); 
		for (var i = 0; i <$("#myTable-order")[0].rows.length; i++) {    //遍历Table的所有Row
		        tableInfo = $("#myTable-order")[0].rows[i].cells[3].innerText;   //获取Table中单元格的内容
		     //  alert(tableInfo);
		        array.push(tableInfo);
		}
		 array.splice(0,1);
		 
		 return array;
}

$(function(){
	$('#myCart').click(function(){
		$.ajax({
			  type:'POST',
			  url:'/movie.front/orderController/getOrderListsByUserId',
			  dataType:'json',
			  contentType:'application/json',
			  success:function(data){
				 if(data.result[0].statusCode!=null){
					  alert(data.result[0].statusCode);
					 window.location.href="account.html";
				 }else{
					 $('#myorders-table').empty();
					 var data =data.result;
					 for(var i=0;i<data.length;i++){
						 var html='';
						 html = html+'<tr><td  align="center">'+data[i].orderName+'</td><td  align="center">'+data[i].orderPrice+'元</td><td  align="center">'+data[i].orderTime+'</td></tr>';
						 $('#myorders-table').append(html);
					 }
				 }
			  }
		});
	});

});
